using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace MONA.Modules.Marketing.BLL
{
    public partial class Contact
    {
        #region Private members.

        protected int _ID;
        protected string _Title = string.Empty;
        protected string _Name = string.Empty;
        protected string _FirstName = string.Empty;
        protected string _MiddleName = string.Empty;
        protected string _LastName = string.Empty;
        protected string _Suffix = string.Empty;
        protected string _HomePhone = string.Empty;
        protected string _CellPhone = string.Empty;
        protected string _Fax = string.Empty;
        protected string _Address1 = string.Empty;
        protected string _Address2 = string.Empty;
        protected string _City = string.Empty;
        protected string _State = string.Empty;
        protected string _Country = string.Empty;
        protected string _ZipCode = string.Empty;
        protected string _EmailAddress = string.Empty;
        protected string _Website = string.Empty;
        protected int _LastContactID;

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Properties.

        public int ID
        {
            set { this._ID = value; }
            get { return this._ID; }
        }

        public string Title
        {
            set { this._Title = value; }
            get { return this._Title; }
        }

        public string Name
        {
            set { this._Name = value; }
            get { return this._Name; }
        }

        public string FirstName
        {
            set { this._FirstName = value; }
            get { return this._FirstName; }
        }

        public string MiddleName
        {
            set { this._MiddleName = value; }
            get { return this._MiddleName; }
        }

        public string LastName
        {
            set { this._LastName = value; }
            get { return this._LastName; }
        }

        public string Suffix
        {
            set { this._Suffix = value; }
            get { return this._Suffix; }
        }

        public string HomePhone
        {
            set { this._HomePhone = value; }
            get { return this._HomePhone; }
        }

        public string CellPhone
        {
            set { this._CellPhone = value; }
            get { return this._CellPhone; }
        }

        public string Fax
        {
            set { this._Fax = value; }
            get { return this._Fax; }
        }

        public string Address1
        {
            set { this._Address1 = value; }
            get { return this._Address1; }
        }

        public string Address2
        {
            set { this._Address2 = value; }
            get { return this._Address2; }
        }

        public string City
        {
            set { this._City = value; }
            get { return this._City; }
        }

        public string State
        {
            set { this._State = value; }
            get { return this._State; }
        }

        public string Country
        {
            set { this._Country = value; }
            get { return this._Country; }
        }

        public string ZipCode
        {
            set { this._ZipCode = value; }
            get { return this._ZipCode; }
        }

        public string EmailAddress
        {
            set { this._EmailAddress = value; }
            get { return this._EmailAddress; }
        }

        public string Website
        {
            set { this._Website = value; }
            get { return this._Website; }
        }

        public int LastContactID
        {
            set { this._LastContactID = value; }
            get { return this._LastContactID; }
        }


        #endregion

        //---------------------------------------------------------------------------------------------

        #region Select methods.

        public static Contact Load(int iD)
        {
            string spName = "[dbo].p_MARKETING_Contact_Load";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, iD);
            Contact entity = null;
            IDataReader reader = db.ExecuteReader(dbCommand);
            if (reader.Read())
            {
                entity = new Contact();
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt32(reader.GetOrdinal("ID"));
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) entity.Title = reader.GetString(reader.GetOrdinal("Title"));
                if (!reader.IsDBNull(reader.GetOrdinal("Name"))) entity.Name = reader.GetString(reader.GetOrdinal("Name"));
                if (!reader.IsDBNull(reader.GetOrdinal("FirstName"))) entity.FirstName = reader.GetString(reader.GetOrdinal("FirstName"));
                if (!reader.IsDBNull(reader.GetOrdinal("MiddleName"))) entity.MiddleName = reader.GetString(reader.GetOrdinal("MiddleName"));
                if (!reader.IsDBNull(reader.GetOrdinal("LastName"))) entity.LastName = reader.GetString(reader.GetOrdinal("LastName"));
                if (!reader.IsDBNull(reader.GetOrdinal("Suffix"))) entity.Suffix = reader.GetString(reader.GetOrdinal("Suffix"));
                if (!reader.IsDBNull(reader.GetOrdinal("HomePhone"))) entity.HomePhone = reader.GetString(reader.GetOrdinal("HomePhone"));
                if (!reader.IsDBNull(reader.GetOrdinal("CellPhone"))) entity.CellPhone = reader.GetString(reader.GetOrdinal("CellPhone"));
                if (!reader.IsDBNull(reader.GetOrdinal("Fax"))) entity.Fax = reader.GetString(reader.GetOrdinal("Fax"));
                if (!reader.IsDBNull(reader.GetOrdinal("Address1"))) entity.Address1 = reader.GetString(reader.GetOrdinal("Address1"));
                if (!reader.IsDBNull(reader.GetOrdinal("Address2"))) entity.Address2 = reader.GetString(reader.GetOrdinal("Address2"));
                if (!reader.IsDBNull(reader.GetOrdinal("City"))) entity.City = reader.GetString(reader.GetOrdinal("City"));
                if (!reader.IsDBNull(reader.GetOrdinal("State"))) entity.State = reader.GetString(reader.GetOrdinal("State"));
                if (!reader.IsDBNull(reader.GetOrdinal("Country"))) entity.Country = reader.GetString(reader.GetOrdinal("Country"));
                if (!reader.IsDBNull(reader.GetOrdinal("ZipCode"))) entity.ZipCode = reader.GetString(reader.GetOrdinal("ZipCode"));
                if (!reader.IsDBNull(reader.GetOrdinal("EmailAddress"))) entity.EmailAddress = reader.GetString(reader.GetOrdinal("EmailAddress"));
                if (!reader.IsDBNull(reader.GetOrdinal("Website"))) entity.Website = reader.GetString(reader.GetOrdinal("Website"));
                if (!reader.IsDBNull(reader.GetOrdinal("LastContactID"))) entity.LastContactID = reader.GetInt32(reader.GetOrdinal("LastContactID"));
            }
            reader.Close();
            dbCommand.Connection.Close();

            return entity;
        }

        //---------------------------------------------------------------------------------------------


        public static DataSet SelectAll()
        {
            string spName = "[dbo].p_MARKETING_Contact_SelectAll";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);


            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderAll()
        {
            string spName = "[dbo].p_MARKETING_Contact_SelectAll";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);


            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
        {
            string spName = "[dbo].p_MARKETING_Contact_SelectDynamic";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteDataSet(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
        {
            string spName = "[dbo].p_MARKETING_Contact_SelectDynamic";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteReader(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Insert methods.

        public static int Insert(string title, string name, string firstName, string middleName, string lastName, string suffix, string homePhone, string cellPhone, string fax, string address1, string address2, string city, string state, string country, string zipCode, string emailAddress, string website, int lastContactID)
        {
            Contact entity = new Contact();
            entity.Title = title;
            entity.Name = name;
            entity.FirstName = firstName;
            entity.MiddleName = middleName;
            entity.LastName = lastName;
            entity.Suffix = suffix;
            entity.HomePhone = homePhone;
            entity.CellPhone = cellPhone;
            entity.Fax = fax;
            entity.Address1 = address1;
            entity.Address2 = address2;
            entity.City = city;
            entity.State = state;
            entity.Country = country;
            entity.ZipCode = zipCode;
            entity.EmailAddress = emailAddress;
            entity.Website = website;
            entity.LastContactID = lastContactID;
            return entity.Insert();
        }

        public int Insert()
        {
            return this.Insert(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Insert(SqlTransaction transaction)
        {
            string spName = "[dbo].p_MARKETING_Contact_Insert";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddOutParameter(dbCommand, "@ID", SqlDbType.Int, 4);
            db.AddInParameter(dbCommand, "@Title", SqlDbType.NVarChar, this._Title);
            db.AddInParameter(dbCommand, "@Name", SqlDbType.NVarChar, this._Name);
            db.AddInParameter(dbCommand, "@FirstName", SqlDbType.NVarChar, this._FirstName);
            db.AddInParameter(dbCommand, "@MiddleName", SqlDbType.NVarChar, this._MiddleName);
            db.AddInParameter(dbCommand, "@LastName", SqlDbType.NVarChar, this._LastName);
            db.AddInParameter(dbCommand, "@Suffix", SqlDbType.NVarChar, this._Suffix);
            db.AddInParameter(dbCommand, "@HomePhone", SqlDbType.NVarChar, this._HomePhone);
            db.AddInParameter(dbCommand, "@CellPhone", SqlDbType.NVarChar, this._CellPhone);
            db.AddInParameter(dbCommand, "@Fax", SqlDbType.NVarChar, this._Fax);
            db.AddInParameter(dbCommand, "@Address1", SqlDbType.NVarChar, this._Address1);
            db.AddInParameter(dbCommand, "@Address2", SqlDbType.NVarChar, this._Address2);
            db.AddInParameter(dbCommand, "@City", SqlDbType.NVarChar, this._City);
            db.AddInParameter(dbCommand, "@State", SqlDbType.NVarChar, this._State);
            db.AddInParameter(dbCommand, "@Country", SqlDbType.NVarChar, this._Country);
            db.AddInParameter(dbCommand, "@ZipCode", SqlDbType.NVarChar, this._ZipCode);
            db.AddInParameter(dbCommand, "@EmailAddress", SqlDbType.NVarChar, this._EmailAddress);
            db.AddInParameter(dbCommand, "@Website", SqlDbType.NVarChar, this._Website);
            db.AddInParameter(dbCommand, "@LastContactID", SqlDbType.Int, this._LastContactID);

            if (transaction != null)
            {
                db.ExecuteNonQuery(dbCommand, transaction);
                this._ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return this._ID;
            }
            else
            {
                db.ExecuteNonQuery(dbCommand);
                this._ID = (int)db.GetParameterValue(dbCommand, "@ID");
                return this._ID;
            }
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Update methods.

        public static int Update(int iD, string title, string name, string firstName, string middleName, string lastName, string suffix, string homePhone, string cellPhone, string fax, string address1, string address2, string city, string state, string country, string zipCode, string emailAddress, string website, int lastContactID)
        {
            Contact entity = new Contact();
            entity.ID = iD;
            entity.Title = title;
            entity.Name = name;
            entity.FirstName = firstName;
            entity.MiddleName = middleName;
            entity.LastName = lastName;
            entity.Suffix = suffix;
            entity.HomePhone = homePhone;
            entity.CellPhone = cellPhone;
            entity.Fax = fax;
            entity.Address1 = address1;
            entity.Address2 = address2;
            entity.City = city;
            entity.State = state;
            entity.Country = country;
            entity.ZipCode = zipCode;
            entity.EmailAddress = emailAddress;
            entity.Website = website;
            entity.LastContactID = lastContactID;
            return entity.Update();
        }

        public int Update()
        {
            return this.Update(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Update(SqlTransaction transaction)
        {
            string spName = "[dbo].p_MARKETING_Contact_Update";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);
            db.AddInParameter(dbCommand, "@Title", SqlDbType.NVarChar, this._Title);
            db.AddInParameter(dbCommand, "@Name", SqlDbType.NVarChar, this._Name);
            db.AddInParameter(dbCommand, "@FirstName", SqlDbType.NVarChar, this._FirstName);
            db.AddInParameter(dbCommand, "@MiddleName", SqlDbType.NVarChar, this._MiddleName);
            db.AddInParameter(dbCommand, "@LastName", SqlDbType.NVarChar, this._LastName);
            db.AddInParameter(dbCommand, "@Suffix", SqlDbType.NVarChar, this._Suffix);
            db.AddInParameter(dbCommand, "@HomePhone", SqlDbType.NVarChar, this._HomePhone);
            db.AddInParameter(dbCommand, "@CellPhone", SqlDbType.NVarChar, this._CellPhone);
            db.AddInParameter(dbCommand, "@Fax", SqlDbType.NVarChar, this._Fax);
            db.AddInParameter(dbCommand, "@Address1", SqlDbType.NVarChar, this._Address1);
            db.AddInParameter(dbCommand, "@Address2", SqlDbType.NVarChar, this._Address2);
            db.AddInParameter(dbCommand, "@City", SqlDbType.NVarChar, this._City);
            db.AddInParameter(dbCommand, "@State", SqlDbType.NVarChar, this._State);
            db.AddInParameter(dbCommand, "@Country", SqlDbType.NVarChar, this._Country);
            db.AddInParameter(dbCommand, "@ZipCode", SqlDbType.NVarChar, this._ZipCode);
            db.AddInParameter(dbCommand, "@EmailAddress", SqlDbType.NVarChar, this._EmailAddress);
            db.AddInParameter(dbCommand, "@Website", SqlDbType.NVarChar, this._Website);
            db.AddInParameter(dbCommand, "@LastContactID", SqlDbType.Int, this._LastContactID);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        //---------------------------------------------------------------------------------------------

        #endregion

        //---------------------------------------------------------------------------------------------

        #region Delete methods.

        public static int Delete(int iD)
        {
            Contact entity = new Contact();
            entity.ID = iD;
            return entity.Delete();
        }

        public int Delete()
        {
            return this.Delete(null);
        }

        //---------------------------------------------------------------------------------------------

        public int Delete(SqlTransaction transaction)
        {
            string spName = "[dbo].p_MARKETING_Contact_Delete";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, this._ID);

            if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
        }

        #endregion
    }
}